
package com.market.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginFilter implements Filter
{
    String url;

    String[] u;

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
            ServletException
    {
        // TODO Auto-generated method stub

        HttpServletRequest req = (HttpServletRequest)request;
        String path = req.getRequestURL().toString();
        HttpServletResponse res = (HttpServletResponse)response;
        if (req.getSession().getAttribute("cus") == null && path.endsWith(".do"))
        {
            for (int i = 0; i < u.length; i++)
            {
                if (path.indexOf(u[i]) != -1)
                {
                    if (req.getSession().getAttribute("cus") == null)
                    {
                        StringBuffer str = new StringBuffer(path);
                        String query = req.getQueryString();
                        if (query != null)
                        {
                            str.append("?").append(query);
                        }
                        res.sendRedirect("./login.jsp?" + str.toString());
                        return;
                    }
                }
            }
        }
        chain.doFilter(request, response);
    }

    public void init(FilterConfig con) throws ServletException
    {
        url = con.getInitParameter("url");
        u = url.split(",");
    }

    public void destroy()
    {
        // TODO Auto-generated method stub

    }
}
